//package com.gzy.studentsociety.filter;
//
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.Filter;
//import javax.servlet.FilterConfig;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.FilterChain;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.net.URLEncoder;
//
//@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
//public class LoginFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest request,
//                         ServletResponse response,
//                         FilterChain chain)
//            throws ServletException, IOException {
//
//        chain.doFilter(request, response);
//
//        HttpServletRequest req = (HttpServletRequest)request;
//        HttpServletResponse res = (HttpServletResponse) response;
//        String url = req.getRequestURI();
////        if("/studentSociety/index.jsp".equals(url) ||
////                "/studentSociety/".equals(url) ||
////                "/studentSociety/student/login".equals(url)){
////            chain.doFilter(req,res);
//
//        if("/studentSociety/index.jsp".equals(url) ||
//                "/studentSociety/".equals(url) ||
//                "/studentSociety/student/login".equals(url) ||
//                url.startsWith("/studentSociety/images/")) {
//            chain.doFilter(req, res);
//        }else{
//            HttpSession session = req.getSession();
//            String student = (String)(session.getAttribute("student"));
//            if(student == null){
//                res.sendRedirect("/studentSociety?message=请登录" + URLEncoder.encode("请登录", "utf-8"));
//            }else{
//                chain.doFilter(req,res);
//            }
//        }
//    }
//}


package com.gzy.studentsociety.filter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

@WebFilter(filterName = "LoginFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String url = req.getRequestURI();

        // 定义不需要登录验证的路径
        boolean isPublicPath =
                "/studentSociety/index.jsp".equals(url) ||
                        "/studentSociety/".equals(url) ||
                        "/studentSociety/student/login".equals(url) ||
                        url.startsWith("/studentSociety/images/") ||
                        url.startsWith("/studentSociety/css/") ||
                        url.startsWith("/studentSociety/js/");

        if (isPublicPath) {
            chain.doFilter(req, res);
        } else {
            HttpSession session = req.getSession();
            String student = (String) session.getAttribute("student");
            if (student == null) {
                res.sendRedirect("/studentSociety?message=" + URLEncoder.encode("请登录", "utf-8"));
            } else {
                // 只有当用户已登录时才继续传递请求
                chain.doFilter(req, res);
            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化方法，如果有需要初始化的操作可以在这里进行
    }

    @Override
    public void destroy() {
        // 销毁方法，如果有需要清理的工作可以在这里进行
    }
}